<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div
      class="filter-container"
      style="float: right">
      <el-input
        v-model="listQuery.userId"
        clearable
        class="filter-item"
        style="width: 200px;"
        placeholder="请输入用户编号"/>
      <el-input
        v-model="listQuery.userName"
        clearable
        class="filter-item"
        style="width: 200px;"
        placeholder="请输入用户名称"/>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter">查找
      </el-button>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate">添加
      </el-button>
    </div>

    <!-- 查询结果 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="正在查询中。。。"
      border
      fit
      highlight-current-row>
      <el-table-column type="expand">
        <!-- element展开行详情模板 -->
        <template slot-scope="props">
          <el-form
            label-position="left"
            class="table-expand">
            <el-form-item label="用户介绍">
              <span>{{ props.row.userExplain }}</span>
            </el-form-item>
            <el-form-item label="备注">
              <span>{{ props.row.remarks }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <!-- element表格 -->
      <el-table-column
        align="center"
        label="用户编号"
        width="80"
        prop="userId"/>

      <el-table-column
        align="center"
        min-width="100"
        label="用户名称"
        prop="userName"/>

      <el-table-column
        align="center"
        min-width="40"
        label="性别"
        prop="userSex">
        <template slot-scope="scope">
          {{ scope.row.userSex | formatGender }}
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        property="iconUrl"
        label="用户图片">
        <template slot-scope="scope">
          <img
            :src="scope.row.userImg"
            width="40">
        </template>
      </el-table-column>

      <!-- <el-table-column
        align="center"
        label="用户详情"
        prop="detail">
        <template slot-scope="scope">
          <el-dialog
            :visible.sync="detailDialogVisible"
            width="60%"
            title="用户详情">
            <div v-html="goodsDetail" />
          </el-dialog>
          <el-button
            type="primary"
            size="mini"
            @click="showDetail(scope.row.userExplain)">查看</el-button>
        </template>
      </el-table-column> -->


      <el-table-column
        align="center"
        label="操作"
        width="260"
        class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="handleUpdate(scope.row)">编辑
          </el-button>
          <el-button
            type="danger"
            size="mini"
            @click="handleDelete(scope.row)">删除
          </el-button>
          <el-button
            type="success"
            size="mini"
            @click="assginRole(scope.row.id)">分配角色
          </el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :pageSizes="[2,5,10,20]"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"/>

    <el-tooltip
      placement="top"
      content="返回顶部">
      <back-to-top :visibility-height="100"/>
    </el-tooltip>

    <el-dialog title="角色分配" :visible.sync="dialogFormVisible">
      <el-form
        ref="roleForm"

        :model="roleForm"
        status-icon
        label-position="left"
        label-width="100px"
        style="width: 400px; margin-left: 50px"
      >
        <el-form-item label="角色名称" prop="name">
          <el-select v-model="roleForm.name">
          <el-option label="超级管理员" value="1"></el-option>
          <el-option label="食品管理员" value="3"></el-option>
          <el-option label="商家管理员" value="2"></el-option>

          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button

          type="primary"
          @click="saveRole()"
        >分配
        </el-button>
      </div>
    </el-dialog>


  </div>
</template>

<style>
.table-expand {
  font-size: 0;
}

.table-expand label {
  width: 100px;
  color: #99a9bf;
}

.table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
}

.gallery {
  width: 80px;
  margin-right: 10px;
}
</style>

<script>
import {orderTypes, getOrderType, formatGender} from '@/filters'
import {listUser, deleteUser,assignRole} from '@/api/system/user'
import BackToTop from '@/components/BackToTop'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'UserList',
  components: {BackToTop, Pagination},
  data() {
    return {
      roleForm:{
        name:""
      },
      dialogFormVisible:false,
      list: [], // 数据列表
      total: 0, // 数据总数
      listLoading: true,
      orderTypes: orderTypes,
      // element-admin 分页
      listQuery: {
        page: 1, // 页码
        limit: 20, // 每页条数
        orderTypeId: undefined, // 点餐类别
        userName: undefined, // 用户名称（支持模糊查询）
        sort: 'add_time',
        order: 'desc'
      },
      // 查看详情数据
      goodsDetail: '',
      detailDialogVisible: false // 模态框状态
    }
  },
  created() {
    console.log(orderTypes);
    // 页面信息初始化
    this.getList()
  },
  methods: {
    saveRole() {

      let userId = this.roleForm.userId.toString()
      let roleId = this.roleForm.name;
      console.log("用户:[" + userId + "]分配角色:[" + roleId + "]成功！......");
      let params = {
        userId,
        roleId,
      };
      assignRole(params)
        .then((response) => {
          this.dialogFormVisible = false;
          this.$notify.success({
            title: "成功",
            message: "更新角色权限成功",
          });
        })
        .catch(() => {
          this.dialogFormVisible = false;
        });
    },
    getOrderType: getOrderType,
    // 获取列表数据
    getList() {
      this.listLoading = true

      let dt = +new Date();
      this.listQuery.dt = dt;
      listUser(this.listQuery)
        .then(response => {
          console.log("-----user list data:%o", response.data.data);
          this.list = response.data.data; // response.data.data.list
          this.total = response.data.total; //response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    // 查询按钮钩子
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    assginRole(userId){
      this.dialogFormVisible = true
      this.roleForm.userId = userId
      console.log(userId+'--------正在分配角色---------');

    },

    // 添加用户按钮跳转
    handleCreate() {
      this.$router.push({path: '/system/user/add'})
    },
    // 编辑用户跳转
    handleUpdate(row) {
      this.$router.push({path: '/system/user/edit', query: {id: row.userId}})
    },
    // 查看用户详情
    showDetail(detail) {
      this.goodsDetail = detail
      this.detailDialogVisible = true
    },
    // 删除用户
    handleDelete(row) {
      this.$confirm('此操作将删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteUser(row.userId)
            .then(response => {
              this.$notify.success({
                title: '成功',
                message: '删除用户成功'
              })
              // 只在前端页面删除该用户信息数据。
              const index = this.list.indexOf(row)
              this.list.splice(index, 1)
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.errmsg
              })
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  },
  filters: {
    formatGender
  }
}
</script>
